package it.polimi.meteocal.web.beans;

import it.polimi.meteocal.ejb.eventmanagement.EventMgr;
import it.polimi.meteocal.ejb.invitationmanager.InvitationDTO;
import it.polimi.meteocal.ejb.invitationmanager.InvitationMgr;
import it.polimi.meteocal.ejb.usermanagement.UserMgr;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean (name="invitationBean")
@SessionScoped
public class InvitationBean {

	@EJB
	private InvitationMgr invitationMgr;

	//users
	private List<?> result;
	//events
	private List<?> result2;
	
	@EJB
	private UserMgr userMgr;
	
	@EJB
	private EventMgr eventMgr;
	
	private InvitationDTO invitation;
	
	private List<String> emails; 
	
	
	public String findAllUsers(){
		result = userMgr.getAllUsers();
		return "invitation?faces-redirect=true";
	}
	
	public String findMyEvents(){
		result2 = eventMgr.findMyEvents();
		return "invitation?faces-redirect=true";
	}
	
	
	public String findByUser(){
		result = invitationMgr.findByUser();
		return "invitation?faces-redirect=true";
	}

	public InvitationMgr getInvitationMgr() {
		return invitationMgr;
	}

	public void setInvitationMgr(InvitationMgr invitationMgr) {
		this.invitationMgr = invitationMgr;
	}

	public List<?> getResult() {
		return result;
	}

	public void setResult(List<?> result) {
		this.result = result;
	}
	
	public List<?> getResult2() {
		return result2;
	}

	public void setResult2(List<?> result2) {
		this.result2 = result2;
	}

	public String createInvitation() {
		invitationMgr.save(invitation,emails);
		result.clear();
		result2.clear();
		return "personal?faces-redirect=true";
	}
	
	public void addUser(String email){
		System.out.println("Email aggiunta alla stringa: "+email);
		emails.add(email);
	}
	
	public void selectEvent(int eventid,String eventName){
		invitation = new InvitationDTO();
		emails = new ArrayList<String>();
		invitation.setEventid(eventid);
		invitation.setTitle("Invite at "+eventName);
	}
	
	public void accept(int invitationid){
		invitationMgr.accept(invitationid);
	}
	
	public void decline(int invitationid){
		invitationMgr.decline(invitationid);
	}
	
}
